package download.itydl.com.sinlethread;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import download.itydl.com.sinlethread.bean.FileInfo;
import download.itydl.com.sinlethread.services.DownLoadService;

/**
 * @author howard
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTvFileName;
    private ProgressBar mProgressBar;
    private Button mStart;
    private Button mStop;
    private static final String downLoadUrl = "http://172.18.198.175:8080/QQ.apk";
    private FileInfo fileInfo;
    private MyReceiver receiver;
    private TextView tvPb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        init();

        // 注册广播接收器
        initReceiver();
    }

    private void initView() {
        mTvFileName = (TextView) findViewById(R.id.tvFileName);
        tvPb = findViewById(R.id.textView);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mStart = (Button) findViewById(R.id.start);
        mStop = (Button) findViewById(R.id.stop);
        mStart.setOnClickListener(this);
        mStop.setOnClickListener(this);
    }

    private void initReceiver() {
        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownLoadService.ACTION_UPDATE);
        registerReceiver(receiver, filter);
    }

    private void init() {
        mProgressBar.setMax(100);
        fileInfo = new FileInfo(0,
                downLoadUrl,
                "QQ.apk",
                0,
                0);
        mTvFileName.setText(fileInfo.getFileName());
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, DownLoadService.class);
        intent.putExtra(DownLoadService.FILE_INFO, fileInfo);
        switch (view.getId()) {
            case R.id.start:
                intent.setAction(DownLoadService.START);
                startService(intent);
                break;
            case R.id.stop:
                // 暂停
                intent.setAction(DownLoadService.PAUSE);
                startService(intent);
                break;
        }
    }

    /**
     * 更新UI的广播
     */
    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (DownLoadService.ACTION_UPDATE.equals(intent.getAction())) {
                // 接收进度更新UI
                int finished = intent.getIntExtra("finished", 0);
                Log.e("yangdaolong", "onReceive: " + finished + "");
                mProgressBar.setProgress(finished);
                tvPb.setText(new StringBuffer().append(finished).append("%"));
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
